package com.sbs.demo.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.ToString;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @author luwenmin
 */
@ToString
public class ResponseData<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 成功CODE
     */
    public static final int SUCCESS_CODE = CommonConstant.SUCCESS_CODE;

    /**
     * 失败 CODE
     */
    public static final int FAIL_CODE = CommonConstant.FAIL;

    /**
     * 成功 STATUS
     */
    public static final int SUCCESS_STATUS = CommonConstant.SUCCESS;

    /**
     * 失败 STATUS
     */
    public static final int FAIL_STATUS = CommonConstant.FAIL;

    /**
     * 成功 MSG
     */
    public static final String SUCCESS_MSG = "success";

    /**
     * 失败 MSG
     */
    public static final String FAIL_MSG = "error";

    private int code;

    private String message;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    private int timestamp;

    public static <T> ResponseData<T> ok() {
        return restResult(null, SUCCESS_CODE, SUCCESS_MSG, SUCCESS_STATUS);
    }

    public static <T> ResponseData<T> ok(T data) {
        return restResult(data, SUCCESS_CODE, SUCCESS_MSG, SUCCESS_STATUS);
    }

    public static <T> ResponseData<T> ok(T data, String msg) {
        return restResult(data, SUCCESS_CODE, msg, SUCCESS_STATUS);
    }

    public static <T> ResponseData<T> fail() {
        return restResult(null, FAIL_CODE, FAIL_MSG, FAIL_STATUS);
    }

    public static <T> ResponseData<T> fail(String msg) {
        return restResult(null, FAIL_CODE, msg, FAIL_STATUS);
    }

    public static <T> ResponseData<T> fail(T data) {
        return restResult(null, FAIL_CODE, FAIL_MSG, FAIL_STATUS);
    }

    public static <T> ResponseData<T> fail(T data, String msg) {
        return restResult(null, FAIL_CODE, msg, FAIL_STATUS);
    }

    public static <T> ResponseData<T> fail(int code, String msg) {
        return restResult(null, code, msg, FAIL_STATUS);
    }

    public static <T> ResponseData<T> fail(int code, String msg, int status) {
        return restResult(null, code, msg, status);
    }

    public static <T> ResponseData<T> fail(int code, String msg, T data) {
        return restResult(data, code, msg, FAIL_STATUS);
    }

    private static <T> ResponseData<T> restResult(T data, int code, String msg, int status) {
        ResponseData<T> apiResult = new ResponseData<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMessage(msg);
        apiResult.setTimestamp((int) (System.currentTimeMillis() / 1000));
        return apiResult;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }


    public int getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(int timestamp) {
        this.timestamp = timestamp;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
